package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.OrderdetailM;
import com.com.ruanmeng.demon.ServiceM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.wxpay.WXPayHttp;

import java.math.BigDecimal;
import java.util.ArrayList;

import alipay.PayToolPayMoney;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 确认订单
 */
public class SureDDActivity extends BaseActivity {

    @BindView(R.id.li_sure_gwc)
    LinearLayout liSureGwc;
    @BindView(R.id.tv_sure_allprice)
    TextView tvSureAllprice;
    @BindView(R.id.cb_check_zhifubao)
    CheckBox cbCheckZhifubao;
    @BindView(R.id.cb_check_weixin)
    CheckBox cbCheckWeixin;
    private ArrayList<ServiceM.RowsBean> temp_gwc;
    private ArrayList<OrderdetailM.ObjectBean.OrderDetailsBean> temp_gwcOrder;
    String payType = "Alipay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sure_dd);
        ButterKnife.bind(this);
        ChangLayTitle("订单确认");
        AddActivity(this);
        LayBack();
        init();
    }

    private void init() {
        if (TextUtils.isEmpty(getIntent().getStringExtra("FromDD"))) {
            temp_gwc = (ArrayList<ServiceM.RowsBean>) getIntent().getSerializableExtra("gwc_list");
            for (int i = 0; i < temp_gwc.size(); i++) {
                View v = getLayoutInflater().inflate(R.layout.layout_suredd, null);
                TextView tv_suredd_name = (TextView) v.findViewById(R.id.tv_suredd_name);
                TextView tv_suredd_price = (TextView) v.findViewById(R.id.tv_suredd_price);
                TextView tv_suredd_times = (TextView) v.findViewById(R.id.tv_suredd_times);
                TextView tv_suredd_allprice = (TextView) v.findViewById(R.id.tv_suredd_allprice);
                tv_suredd_name.setText(temp_gwc.get(i).getTitle());
                tv_suredd_price.setText(temp_gwc.get(i).getPrice() + "元/次");
                tv_suredd_times.setText(temp_gwc.get(i).getNum() + "次");
                double allprice = temp_gwc.get(i).getNum() * Double.parseDouble(temp_gwc.get(i).getPrice());
                BigDecimal bg = new BigDecimal(allprice);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                tv_suredd_allprice.setText("¥" + f1);
                liSureGwc.addView(v);
            }
            getAllPrice();
        } else {//来自我的订单
            temp_gwcOrder = (ArrayList<OrderdetailM.ObjectBean.OrderDetailsBean>) getIntent().getSerializableExtra("gwc_list");
            for (int i = 0; i < temp_gwcOrder.size(); i++) {
                View v = getLayoutInflater().inflate(R.layout.layout_suredd, null);
                TextView tv_suredd_name = (TextView) v.findViewById(R.id.tv_suredd_name);
                TextView tv_suredd_price = (TextView) v.findViewById(R.id.tv_suredd_price);
                TextView tv_suredd_times = (TextView) v.findViewById(R.id.tv_suredd_times);
                TextView tv_suredd_allprice = (TextView) v.findViewById(R.id.tv_suredd_allprice);
                tv_suredd_name.setText(temp_gwcOrder.get(i).getTitle());
                tv_suredd_price.setText(temp_gwcOrder.get(i).getProPrice() + "元/次");
                tv_suredd_times.setText(temp_gwcOrder.get(i).getProNum() + "次");
                double allprice = temp_gwcOrder.get(i).getProNum() * Double.parseDouble(temp_gwcOrder.get(i).getProPrice()+"");
                BigDecimal bg = new BigDecimal(allprice);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                tv_suredd_allprice.setText("¥" + f1);
                liSureGwc.addView(v);
            }
            getOrdrAllPrice();
        }

    }


    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.ll_weixin:
                payType = "WxPay";
                cbCheckZhifubao.setChecked(false);
                if (cbCheckWeixin.isChecked() == false) {
                    cbCheckWeixin.setChecked(true);
                }
                break;
            case R.id.ll_zhifubao:
                payType = "Alipay";
                cbCheckWeixin.setChecked(false);
                if (cbCheckZhifubao.isChecked() == false) {
                    cbCheckZhifubao.setChecked(true);
                }
                break;
            case R.id.cb_check_zhifubao:
                payType = "Alipay";
                cbCheckWeixin.setChecked(false);
                if (cbCheckZhifubao.isChecked() == false) {
                    cbCheckZhifubao.setChecked(true);
                }
                break;
            case R.id.cb_check_weixin:
                payType = "WxPay";
                cbCheckWeixin.setChecked(false);
                if (cbCheckZhifubao.isChecked() == false) {
                    cbCheckZhifubao.setChecked(true);
                }
                break;
            case R.id.tv_sure_sure://提交支付
                if ("Alipay".equals(payType)) {
                    PayToolPayMoney.getInstance().startPay(this, new PayToolPayMoney.PayCallback() {
                        @Override
                        public void doWork(String payResult, boolean isDone) {
                            if (TextUtils.equals("9000", payResult)) {
                                CommonUtil.showToask(SureDDActivity.this, "支付成功！");
                                Intent intent = new Intent(SureDDActivity.this, ZhiFuSucessActivity.class);
                                startActivity(intent);
                            } else {
                                CommonUtil.showToask(SureDDActivity.this, "支付失败！");
                            }
                        }
                    }, new String[]{PreferencesUtils.getString(SureDDActivity.this, "uid"), payType, getIntent().getStringExtra("orderId")});
                } else {
                    WXPayHttp.getInstance().WeixinPay(this, new String[]{PreferencesUtils.getString(SureDDActivity.this, "uid"), payType, getIntent().getStringExtra("orderId")});
                }
                break;
        }
    }

    /**
     * 获取总价
     */
    public void getAllPrice() {
        double allprice = 0.00;
        for (int i = 0; i < temp_gwc.size(); i++) {
            if (temp_gwc.get(i).getNum() > 0) {
                allprice += temp_gwc.get(i).getNum() * Double.parseDouble(temp_gwc.get(i).getPrice());
            }
        }
        BigDecimal bg = new BigDecimal(allprice);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        tvSureAllprice.setText(f1 + "");
    }
    /**
     * 获取总价
     */
    public void getOrdrAllPrice() {
        double allprice = 0.00;
        for (int i = 0; i < temp_gwcOrder.size(); i++) {
            if (temp_gwcOrder.get(i).getProNum() > 0) {
                allprice += temp_gwcOrder.get(i).getProNum() * Double.parseDouble(temp_gwcOrder.get(i).getProPrice()+"");
            }
        }
        BigDecimal bg = new BigDecimal(allprice);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        tvSureAllprice.setText(f1 + "");
    }
}
